package at.fhooe.im440.pandora.collision;

import java.util.ArrayList;

import at.fhooe.im440.pandora.core.Core;
import at.fhooe.im440.pandora.update.Updatable;


public class CollisionTester implements Updatable {
	
	public ArrayList<CircleCollider> m_Enemies;
	private ArrayList<CircleCollider> m_Bullets;
	private CircleCollider m_PlayerObject;
	private Core m_Core;
	
	public CollisionTester(Core core)
	{
		m_Core = core;
		m_Enemies = new ArrayList<CircleCollider>();
		m_Bullets = new ArrayList<CircleCollider>();
		setUpUpdateable();
	}
	
	public void addBulletObject(CircleCollider bullet)
	{
		m_Bullets.add(bullet);
	}
	
	public void addPlayerObject(CircleCollider player)
	{
		m_PlayerObject = player;
	}

	public void addCollisionObject(CircleCollider obj)
	{
		if(!m_Enemies.contains(obj))
		{
			m_Enemies.add(obj);
		}
		else
		{
			System.err.println("Objekt bereits im CollisionManager");
		}
	}
	
	public void removeBulletObject(CircleCollider bul)
	{
		if(m_Bullets.contains(bul))
		{
			m_Bullets.remove(bul);
		}
	}
	
	public void removeCollisionObject(CircleCollider obj)
	{
		if(m_Enemies.contains(obj))
		{
			m_Enemies.remove(obj);
		}
	}

	@Override
	public void update() {
		//System.out.println("Anzahl der Collisionsobjecte: " + m_Objects.size());
		//Player-Enemy Collision
		for(int i = 0; i < m_Enemies.size(); i++)
		{
			if(checkCollisionWithPlayer(m_Enemies.get(i)))
			{
				m_PlayerObject.collided(m_Enemies.get(i));
				m_Enemies.get(i).collided(m_PlayerObject);
			}
		}
		
		//Bullet-Enemy Collision
		for(int i = 0; i < m_Enemies.size(); i++)
		{
			for(int j = 0; j < m_Bullets.size(); j++)
			{
				if(checkCollisionBulletAsteroid(m_Bullets.get(j), m_Enemies.get(i)))
				{
					m_Bullets.get(j).collided(m_Enemies.get(i));
					m_Enemies.get(i).collided(m_Bullets.get(j));
				}
			}
		}
	}

	private boolean checkCollisionWithPlayer(CircleCollider col) {
		//System.out.println("PX: " + m_PlayerObject.m_X + " | Py: " + m_PlayerObject.m_Y + " | Rad: " + m_PlayerObject.m_Radius);
		//System.out.println("EX: " + col.m_X + " | EY: " + col.m_Y + " | ER: " + col.m_Radius);
		
		if( ((m_PlayerObject.m_X - col.m_X-col.m_Radius/2.0)*(m_PlayerObject.m_X - col.m_X-col.m_Radius/2.0)) < ((m_PlayerObject.m_Radius/2.0f + col.m_Radius/2.0f)*(m_PlayerObject.m_Radius/2.0f + col.m_Radius/2.0f)) &&
			((m_PlayerObject.m_Y - col.m_Y-col.m_Radius/2.0)*(m_PlayerObject.m_Y - col.m_Y-col.m_Radius/2.0)) < ((m_PlayerObject.m_Radius/2.0f + col.m_Radius/2.0f)*(m_PlayerObject.m_Radius/2.0f + col.m_Radius/2.0f)) )
		{
			return true;
		}
		return false;
	}
	
	private boolean checkCollisionBulletAsteroid(CircleCollider bullet, CircleCollider asteroid)
	{
		if( ((bullet.m_X - asteroid.m_X-asteroid.m_Radius/2.0f)*(bullet.m_X - asteroid.m_X-asteroid.m_Radius/2.0f)) < ((bullet.m_Radius/2.0f + asteroid.m_Radius/2.0f)*(bullet.m_Radius/2.0f + asteroid.m_Radius/2.0f)) &&
			((bullet.m_Y - asteroid.m_Y-asteroid.m_Radius/2.0f)*(bullet.m_Y - asteroid.m_Y-asteroid.m_Radius/2.0f)) < ((bullet.m_Radius/2.0f + asteroid.m_Radius/2.0f)*(bullet.m_Radius/2.0f + asteroid.m_Radius/2.0f)) )
		{
			return true;
		}
		return false;
	}

	@Override
	public void setUpUpdateable() {
		m_Core.updateManager.addClass(this);
	}
	
	
}
